package org.jeecg.modules.word_order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.word_order.entity.WorkOrder;
import org.jeecg.modules.word_order.entity.WorkService;
import org.jeecg.modules.word_order.mapper.WorkOrderMapper;
import org.jeecg.modules.word_order.mapper.WorkServiceMapper;
import org.jeecg.modules.word_order.service.IWorkServiceService;
import org.jeecg.modules.word_order.modal.WorkServiceModel;
import org.jeecg.modules.word_order.vo.WorkServiceVo;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;

/**
 * @Description: work_service
 * @Author: jeecg-boot
 * @Date:   2021-08-17
 * @Version: V1.0
 */
@Service
public class WorkServiceServiceImpl extends ServiceImpl<WorkServiceMapper, WorkService> implements IWorkServiceService {

    @Resource
    private WorkServiceMapper serviceMapper;
    @Resource
    private WorkOrderMapper OrderMapper;
    @Override
    public IPage<WorkServiceVo> getWorkServiceByPage(Page<WorkServiceVo> page,QueryWrapper<WorkServiceVo> queryWrapper) {
        return serviceMapper.getWorkServiceByPage(page,queryWrapper);
    }

    @Override
    public void add(WorkServiceModel workServiceModel) {
        WorkService workService = new WorkService();
        //生成工单编号id
        long timestamp = System.currentTimeMillis();  //时间戳
        String number = RandomStringUtils.randomNumeric(5); //生成随机数
        workService.setServiceWorkId("g"+timestamp+number); //工单编号
        workService.setServiceContent(workServiceModel.getServiceContent());   //工单内容
        workService.setServiceEquipment(workServiceModel.getServiceEquipment());   //损坏设备
        workService.setServicePeriod(1); //是否质保期内
        workService.setServiceCharge(1); //是否收费
        workService.setWorkPhoneConfirm(0);
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        workService.setCreateBy(sysUser.getUsername());

        WorkOrder workOrder = new WorkOrder();
        workOrder.setWorkNo("g"+timestamp+number);
        workOrder.setWorkName(workServiceModel.getWorkName());
        workOrder.setWorkCustomer(workServiceModel.getWorkCustomer());
        workOrder.setWorkPhone(workServiceModel.getWorkPhone());
        workOrder.setWorkAddress(workServiceModel.getWorkAddress());
        workOrder.setWorkDistribution(1);
        workOrder.setWorkUrgent(workServiceModel.getWorkUrgent());
        workOrder.setWorkType("3");
        workOrder.setWorkStart(workServiceModel.getWorkStart());
        workOrder.setCreateBy(sysUser.getUsername());
        serviceMapper.insert(workService);
        OrderMapper.insert(workOrder);
    }
}
